<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
$data["LottoID_n"] = (empty($_data["LottoID"]))?$_data["LottoID"]:$_data["LottoID"];
$data["Bet_Type_n"] = (empty($_data["Bet_Type"]))?$data["Bet_Type_n"]:$_data["Bet_Type"];
$data["PayupDate_x"][0] = (empty($_data["PayupDate_0"]))?date("Y-m-d"):$_data["PayupDate_0"];
$data["PayupDate_x"][1] = (empty($_data["PayupDate_1"]))?date("Y-m-d"):$_data["PayupDate_1"];
if(!empty($_data["GambleID"])){
	$arrGD = getGambleDataByID($_data["GambleID"]);
	$data["PayupDate_x"][0] = $arrGD["GameDate"];
	$data["PayupDate_x"][1] = $arrGD["GameDate"];
	$data["GambleID_n"] = $_data["GambleID"];
}
$intReportType = (int)$_data["ReportType"];

if($_data["Types"] != ""){
	$data["StockID_n"] = $_data["StockID"];
	$data["MainID_n"] = $_data["MainID"];
}
$data["isPayOff_n"] = 0;
//$data["WagerType_n"] = 0;
if($isLimit){
	$data["PayupDate_x"][0] = getPayupDateOfAccountSearchLimit($data["PayupDate_x"][0], $isSevenDay);
	if($data["PayupDate_x"][0] > $data["PayupDate_x"][1]){
		$GLOBALS["errormessage"] = "Over search date range!!";
		ErrMsgOutputHTML("Error", "", 1);
	}
}else{
	if($data["PayupDate_x"][0] > $data["PayupDate_x"][1]){
		$tmp = $data["PayupDate_x"][1];
		$data["PayupDate_x"][1] = $data["PayupDate_x"][0];
		$data["PayupDate_x"][0] = $tmp;
	}
}
$strSQL = SelectSQL ($data, "`bet_BetList`");// USE INDEX (PayupDate)

if($intReportType == 0){
	$n = "select SubID, MemID, COUNT(*) as totals, SUM(MemStake) as MemStake, SUM(SubIncome) as SubIncome, SUM(SubResult) as SubResult, MemExchangeRate, SubRate, SUM(MemStake*MemExchangeRate*SubRate) as RealMemStake, SUM(SubRateResult) as SubRateResult, SUM(MemResult*MemExchangeRate*SubRate) as RealMemResult, SUM(SubIncome - MemResult) as RealMemCommission, SUM(IF(MemResult = 0, 0, MemStake)*MemExchangeRate*SubRate*-1*(SubCommission - MemCommission)) as SubComm";  //MemStake*ROUND(MemCommission, 4)
	$strSQL = str_replace("select *", $n, $strSQL);
	$strSQL1 = $strSQL;
	$strSQL .=" and WagerType in (0) group by MemID order by MemID limit 0, 500";
	$strSQL1 .=" and WagerType in (2) group by MemID order by MemID limit 0, 500";
}elseif($intReportType == 1){
	$n = "select LottoID, Bet_Type, SubID, COUNT(*) as totals, SUM(MemStake) as MemStake, SUM(SubIncome) as SubIncome, SUM(SubResult) as SubResult, MemExchangeRate, SubRate, SUM(MemStake*MemExchangeRate*SubRate) as RealMemStake, SUM(SubRateResult) as SubRateResult, SUM(MemResult*MemExchangeRate*SubRate) as RealMemResult, SUM(SubIncome - MemResult) as RealMemCommission, SUM(IF(MemResult = 0, 0, MemStake)*MemExchangeRate*SubRate*-1*(SubCommission - MemCommission)) as SubComm";  //MemStake*ROUND(MemCommission, 4)
	$strSQL = str_replace("select *", $n, $strSQL);
	$strSQL1 = $strSQL;
	$strSQL .=" and WagerType in (0) group by LottoID, Bet_Type limit 0, 500";
	$strSQL1 .=" and WagerType in (2) group by LottoID, Bet_Type limit 0, 500";
}

if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Agent Report Error";
	$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\n".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

$arrRows = array();
$arrUserID = array();
$arrAccount = array();
//一般帳
while($Rows = PMA_mysql_fetch_object($RS)){
	$arrRows[0][] = $Rows;
	$arrUserID[] = $Rows -> SubID;
	if($intReportType == 0)$arrUserID[] = $Rows -> MemID;
}

//向上補貨帳
if($strSQL1 != ""){
	if(!$RS = PMA_mysql_query($strSQL1, $$_Conn)){
		$GLOBALS["errormessage"] = "Agent Report Error";
		$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}
	while($Rows = PMA_mysql_fetch_object($RS)){
		$arrRows[1][] = $Rows;
		$arrUserID[] = $Rows -> SubID;
	}
}

//取得使用者 Account
if(sizeof($arrUserID) > 0){
	$strSQL = "select ID, Account, Name from `bet_Member` where ID in (".join(",", $arrUserID).")";
	if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
		$GLOBALS["errormessage"] = "Find User Data Error";
		$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\n".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}

	while($Rows = PMA_mysql_fetch_object($RS)){
		$arrAccount[$Rows -> ID] = ($_KSysParam[ReportIsShowName]==0)?$Rows -> Account:$Rows -> Account."<a style='color: #336600'>(".$Rows -> Name.")</a>";
	}
}
?>